package com.cnhis.cloudhealth.clinical.hosdoctor.doctororderstop.bo;

import com.cnhis.cloudhealth.clinical.hosdoctor.doctororderreset.dao.DoctororderresetDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DoctororderstopBo {
	@Resource
	private DoctororderresetDao doctororderresetDao;
	@Autowired
	private GetNewId getNewId;

	private List<Map<String,String>> changeToString(List<?> list){
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		for(Object o:list){
			if(o instanceof Map){
				Map<String,Object> pmap = (Map<String,Object>)o;
				Map<String,String> smap = new HashMap<String,String>();
				Iterator<Map.Entry<String, Object>> it = pmap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<String, Object> entry = it.next();
					String str = String.valueOf(entry.getValue());
					smap.put(entry.getKey(),str);
				}
				resultList.add(smap);
			}
		}
		return resultList;
	}

	private ModelVo packModelVo(String nid, Map map){
		ModelVo mo = new ModelVo();
		List<?> list = null;
		try {
			list = doctororderresetDao.queryForList(nid,map);
			list = changeToString(list);
		} catch (Exception e) {
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResultMsg("数据操作异常！");
			e.printStackTrace();
		}
		if(list!=null&&list.size()>0) {
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(list.size());
			mo.setRows(list);
		}
		return mo;
	}
	public ModelVo selectPhosstatus(Map<String,Object> map){return packModelVo("Doctororderstop.6419",map);}
	public ModelVo selectPstopedorder(Map<String,Object> map){return packModelVo("Doctororderstop.1297",map);}
	private ModelVo getLongortemporderlist(String nid,String nid2,Map<String,Object> map)throws Exception{
		Map<String,Object> twoparams = (Map<String,Object>)doctororderresetDao.queryToObject(nid,map);
		String VAA04 = MapUtils.getString(twoparams,"vaa04");
		String BCQ04B = MapUtils.getString(twoparams,"bcq04b");
		map.put("VAA04",VAA04);
		map.put("BCQ04B",BCQ04B);
		return packModelVo(nid2,map);
	}
	public ModelVo selectPtemporder(Map<String,Object> map) throws Exception{
		return getLongortemporderlist("Doctororderstop.1298_001","Doctororderstop.1298_002",map);
	}
	public ModelVo selectPlongorder(Map<String,Object> map) throws Exception{
		return getLongortemporderlist("Doctororderstop.1299_001","Doctororderstop.1299_002",map);
	}
	public ModelVo stopOrder(Map<String,Object> map) throws Exception{
		String lXml = MapUtils.getString(map,"lXml","");
		if(StringUtils.isBlank(lXml)){
			return new ModelVo(StaticKeys.OH_FALSE, "参数为空，请检查参数！");
		}
		//将Json数据装换为list
		List<Map<String,Object>> lxmlList = JsonUtil.getList(lXml);
		if(lxmlList==null|| lxmlList.size()==0){
			return new ModelVo(StaticKeys.OH_FALSE, "参数解析有误，请检查！");
		}
		map.put("lxmlList",lxmlList);
		//判断病人状态
		Integer ljcn = (Integer)doctororderresetDao.queryToObject("Doctororderstop.canceltemp_0011",map);
		if(ljcn!=null&&ljcn>3){
			return new ModelVo(StaticKeys.OH_FALSE, "病人当前状态不是住院,无法停止医嘱!");
		}
		String BCE03 = (String)doctororderresetDao.queryToObject("Doctororderstop.stoporder_001",map);
		map.put("lBCE03",BCE03);
		String aSmsg = (String)doctororderresetDao.queryToObject("Doctororderstop.stoporder_002",map);
		if(StringUtils.isNotBlank(aSmsg)){
			return new ModelVo(StaticKeys.OH_FALSE, "'"+aSmsg+"'医嘱已经停止，请返回刷新条件重新选择！");
		}
		map.put("lDate",new Date());
		map.put("lTxt","医嘱停止");
		//更新医嘱表
		doctororderresetDao.update("Doctororderstop.stoporder_003",map);
		Map<String,Object> ids = (Map<String,Object>)doctororderresetDao.queryToObject("Doctororderstop.stoporder_004",map);
		Long lMaxId = MapUtils.getLong(ids,"lmaxid");
		Long lMinId = MapUtils.getLong(ids,"lminid");
		Long lID = getNewId.nextId();
		lID = lID-lMaxId;
		map.put("lMaxId",lMaxId);
		map.put("lMinId",lMinId);
		map.put("lID",lID);
		//插入病人医嘱状态
		doctororderresetDao.insert("Doctororderstop.stoporder_005",map);
		//更新病人医嘱提醒
		doctororderresetDao.update("Doctororderstop.stoporder_006",map);
		return new ModelVo(StaticKeys.OH_SUCCESS, "停嘱成功！");
	}
	//Clinic_Advice_BlankOut
	public ModelVo cancelTempOrder(Map<String,Object> map) throws Exception{
		String lXml = MapUtils.getString(map,"lXml","");
		if(StringUtils.isBlank(lXml)){
			return new ModelVo(StaticKeys.OH_FALSE, "参数为空，请检查参数！");
		}
		//将Json数据装换为list
		List<Map<String,Object>> lxmlList = JsonUtil.getList(lXml);
		if(lxmlList==null|| lxmlList.size()==0){
			return new ModelVo(StaticKeys.OH_FALSE, "参数解析有误，请检查！");
		}
		map.put("lxmlList",lxmlList);
		String BCE03 = (String)doctororderresetDao.queryToObject("Doctororderstop.canceltemp_001",map);
		map.put("lBCE03",BCE03);
		map.put("lDate",new Date());
		//判断病人状态
		Integer ljcn = (Integer)doctororderresetDao.queryToObject("Doctororderstop.canceltemp_0011",map);
		if(ljcn!=null&&ljcn>3){
			return new ModelVo(StaticKeys.OH_FALSE, "病人当前状态不是住院,无法取消医嘱!");
		}
		//判断是否还要医嘱对应费用没冲销
		String aSmsg = (String)doctororderresetDao.queryToObject("Doctororderstop.canceltemp_002",map);
		if(StringUtils.isNotBlank(aSmsg)){
			return new ModelVo(StaticKeys.OH_FALSE, "'"+aSmsg+"'临嘱对应费用还没冲销，请返回先冲销对应费用！");
		}
		//判断手术类的费用
		Integer jcn = (Integer)doctororderresetDao.queryToObject("Doctororderstop.canceltemp_003",map);
		if(jcn!=null&&jcn>0){
			return new ModelVo(StaticKeys.OH_FALSE, "手术临嘱对应医技记账费用还没冲销，请返回先冲销对应费用.");
		}
		
		//判断手术类的状态
		String aSmsg2 = (String)doctororderresetDao.queryToObject("Doctororderstop.canceltemp_004",map);
		if(StringUtils.isNotBlank(aSmsg)){
			return new ModelVo(StaticKeys.OH_FALSE, "'"+aSmsg2+"'手术室已经接收安排手术，不能取消.");
		}
		//判断是否已经取消
		String aSmsg3 = (String)doctororderresetDao.queryToObject("Doctororderstop.canceltemp_005",map);
		if(StringUtils.isNotBlank(aSmsg)){
			return new ModelVo(StaticKeys.OH_FALSE, "'"+aSmsg3+"'临嘱已经取消，请返回重新选择！");
		}
		Map<String,Object> ids = (Map<String,Object>)doctororderresetDao.queryToObject("Doctororderstop.canceltemp_006",map);
		Long lMaxId = MapUtils.getLong(ids,"lmaxid");
		Long lMinId = MapUtils.getLong(ids,"lminid");
		Long lID = getNewId.nextId();
		lID = lID-lMaxId;
		map.put("lMaxId",lMaxId);
		map.put("lMinId",lMinId);
		map.put("lID",lID);
		//更新医嘱记录表（VAF2)
		doctororderresetDao.update("Doctororderstop.canceltemp_007",map);
		//更新医嘱抄送表（VBI2)
		doctororderresetDao.update("Doctororderstop.canceltemp_008",map);
		//删除VAJT
		doctororderresetDao.delete("Doctororderstop.canceltemp_009",map);
		//插入病人医嘱状态
		doctororderresetDao.insert("Doctororderstop.canceltemp_010",map);
		//更新病人医嘱提醒
		doctororderresetDao.update("Doctororderstop.canceltemp_011",map);
		return new ModelVo(StaticKeys.OH_SUCCESS, "临嘱取消成功！");
	}
	public ModelVo selectNurseSureexe(Map<String,Object> map){return packModelVo("Doctororderstop.1305",map);}

	public ModelVo confirmTempOrder(Map<String,Object> map) throws Exception{
		String lXml = MapUtils.getString(map,"lXml","");
		if(StringUtils.isBlank(lXml)){
			return new ModelVo(StaticKeys.OH_FALSE, "参数为空，请检查参数！");
		}
		//将Json数据装换为list
		List<Map<String,Object>> lxmlList = JsonUtil.getList(lXml);
		if(lxmlList==null|| lxmlList.size()==0){
			return new ModelVo(StaticKeys.OH_FALSE, "参数解析有误，请检查！");
		}
		map.put("lxmlList",lxmlList);
		List<Map<String,Object>> list = doctororderresetDao.queryForList("Doctororderstop.confirmorder_001",map);
		if(list!=null&&list.size()>0){
			return new ModelVo(StaticKeys.OH_FALSE, "已有医嘱记录状态发生变化，请返回重新选择过滤条件！");
		}

		map.put("lDate",new Date());
		map.put("lVAF10",9);
		map.put("lVBG06","确认停嘱");
		//更新医嘱记录表（VAF2)
		doctororderresetDao.update("Doctororderstop.confirmorder_002",map);
		Long vbgid = getNewId.nextId();
		map.put("vbgid",vbgid);
		//插入病人医嘱状态
		doctororderresetDao.insert("Doctororderstop.confirmorder_003",map);
		//更新病人医嘱提醒
		doctororderresetDao.update("Doctororderstop.confirmorder_004",map);
		return new ModelVo(StaticKeys.OH_SUCCESS, "临嘱确认停嘱成功！");
	}
}
